import axios from 'axios'

// 获取定位城市的函数 getCurrentCity
export const getCurrentCity = (): any => {
  // 判断localStorage 中是否有定位城市
  const localCity = JSON.parse(localStorage.getItem('hkzf_city') || 'false')
  
  if (!localCity) {
    // 如果没有，就使用获取城市定位 来获取，并且储存到本地储存中，然后返回该城市数据
    return new Promise((resolve, reject) => {
      try {
        const curCity = new window.BMapGL.LocalCity()
        curCity.get(async res => {
          // console.log(res.name)
          const result = await axios.get(`http://127.0.0.1:8080/area/info?name=${res.name}`).then(res => res.data.body)
          // console.log(result)
          localStorage.setItem('hkzf_city', JSON.stringify(result))
          resolve(result)
        })
      } catch(err) {
        reject(err)
      }
      
    })
   
  }
  return Promise.resolve(localCity)
}

export function cube (): any{
  // const localCity = JSON.parse(localStorage.getItem('hkzf_city') || '')
  // console.log(localCity)
  return Promise.resolve(11)
}